const http = require('../../../utils/http.js'); //相对路径
Page({
  /**
   * 页面的初始数据
   */
  data: {
    min: 0,
    zhankai: false,
    daijiesuanshow: true,
    shouhuanmalod: false,
    max: 200,
    clearingRes: '', // 完工提交的数据
    workRes: '',  //工单数据
    currentPrice: '', //实收金额
    modalShow: false, //选择储值卡
    ipx: wx.getStorageSync('ipx'),
    vipCar: {},    //储值卡数据
    vipID: '',  //储值卡ID
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.work_id && options.work_number) {
      // 获取工单收款的数据
      this.getwork(options.work_id, options.work_number);
    } else if(options.documentnumber && options.documentid) {
      this.getVip(options.documentnumber, options.documentid);
    }
  },
  daijiesuanshow() {
    this.setData({
      daijiesuanshow: false
    })
  },
  daijiesuanhide() {
    this.setData({
      daijiesuanshow: true
    })
  },
  shouhuanmalod() {
    this.setData({
      shouhuanmalod: true
    })
  },
  noshouhuanmalod() {
    this.setData({
      shouhuanmalod: false
    })
  },
  // 单选
  radioChange(e) {
    this.setData({
      vipID: e.detail.value
    })
  },
  // 现金输入款
  pay1(e) {
    let clearingRes = this.data.clearingRes;
    clearingRes.cash = e.detail.value;
    clearingRes.total = (Number(e.detail.value) + Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.wechat_pay) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    this.setData({
      clearingRes: clearingRes
    })
  },
  focus1() {
    let clearingRes = JSON.parse(JSON.stringify(this.data.clearingRes));
    let currentPrice = JSON.parse(JSON.stringify(this.data.currentPrice));
    let num = currentPrice -  (Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.wechat_pay) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    clearingRes.cash = num;
    clearingRes.total = currentPrice;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 刷卡
  pay2(e) {
    let clearingRes = this.data.clearingRes;
    clearingRes.swiping_bank_card = e.detail.value;
    clearingRes.total = (Number(e.detail.value) + Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.wechat_pay) + Number(clearingRes.cash) + Number(clearingRes.ali_pay)).toFixed(2);
    this.setData({
      clearingRes: clearingRes
    })
  },
  focus2() {
    let clearingRes = this.data.clearingRes;
    let currentPrice = this.data.currentPrice;
    let num = currentPrice - (Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.wechat_pay) + Number(clearingRes.cash) + Number(clearingRes.ali_pay)).toFixed(2);
    clearingRes.swiping_bank_card = num;
    clearingRes.total = currentPrice;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 微信
  pay3(e) {
    let clearingRes = this.data.clearingRes;
    clearingRes.wechat_pay = e.detail.value;
    clearingRes.total = (Number(e.detail.value) + Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.cash) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    this.setData({
      clearingRes: clearingRes
    })
  },
  focus3() {
    let clearingRes = this.data.clearingRes;
    let currentPrice = this.data.currentPrice;
    let num = currentPrice - (Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.cash) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    clearingRes.wechat_pay = num;
    clearingRes.total = currentPrice;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 支付宝
  pay4(e) {
    let clearingRes = this.data.clearingRes;
    clearingRes.ali_pay = e.detail.value;
    clearingRes.total = (Number(e.detail.value) + Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.wechat_pay) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.cash)).toFixed(2);
    this.setData({
      clearingRes: clearingRes
    })
  },
  focus4() {
    let clearingRes = this.data.clearingRes;
    let currentPrice = this.data.currentPrice;
    let num = currentPrice - (Number(clearingRes.bank_transfer) + Number(clearingRes.other_pay) + Number(clearingRes.wechat_pay) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.cash)).toFixed(2);
    clearingRes.ali_pay = num;
    clearingRes.total = currentPrice;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 银行
  pay5(e) {
    let clearingRes = this.data.clearingRes;
    clearingRes.bank_transfer = e.detail.value;
    clearingRes.total = (Number(e.detail.value) + Number(clearingRes.wechat_pay) + Number(clearingRes.other_pay) + Number(clearingRes.cash) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    this.setData({
      clearingRes: clearingRes
    })
  },
  focus5() {
    let clearingRes = this.data.clearingRes;
    let currentPrice = this.data.currentPrice;
    let num = currentPrice - (Number(clearingRes.wechat_pay) + Number(clearingRes.other_pay) + Number(clearingRes.cash) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    clearingRes.bank_transfer = num;
    clearingRes.total = currentPrice;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 其他
  pay6(e) {
    let clearingRes = this.data.clearingRes;
    clearingRes.other_pay = e.detail.value;
    clearingRes.total = (Number(e.detail.value) + Number(clearingRes.wechat_pay) + Number(clearingRes.bank_transfer) + Number(clearingRes.cash) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    this.setData({
      clearingRes: clearingRes
    })
  },
  focus6() {
    let clearingRes = this.data.clearingRes;
    let currentPrice = this.data.currentPrice;
    let num = currentPrice - (Number(clearingRes.wechat_pay) + Number(clearingRes.bank_transfer) + Number(clearingRes.cash) + Number(clearingRes.swiping_bank_card) + Number(clearingRes.ali_pay)).toFixed(2);
    clearingRes.other_pay = num;
    clearingRes.total = currentPrice;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 优惠
  discount(e) {
    let clearingRes = this.data.clearingRes;
    if(e.detail.value > this.data.workRes.userData.settle_discounts_limit) {
      clearingRes.other_pay = this.data.workRes.userData.settle_discounts_limit;
    } else {
      clearingRes.other_pay = e.detail.value;
    }
    clearingRes.discount -= e.detail.value;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 备注
  remark(e){
    let clearingRes = this.data.clearingRes;
    clearingRes.remark = e.detail.value;
    this.setData({
      clearingRes: clearingRes
    })
  },
  // 展开交易内容
  zhankai() {
    let zhankai = this.data.zhankai
    zhankai = !zhankai
    this.setData({
      zhankai: zhankai
    })
  },
  // 展开储值卡弹窗
  modalShow() {
    if(this.data.workRes.storeValueData.length) {
      this.setData({
        modalShow: true
      })
    } else {
      wx:wx.navigateTo({
        url: '/pages/openCard/openCard/openCard?mobile=' + this.data.workRes.workData.owner.mobile
      })
    }
  },
  // 确定选择储值卡
  probtn() {
    let vipID = this.data.vipID;
    let vipCar = this.data.vipCar;
    console.log(vipID)
    let clearingRes = this.data.clearingRes;
    for(let i in this.data.workRes.storeValueData) {
      if(this.data.workRes.storeValueData[i].id == vipID) {
        vipCar = this.data.workRes.storeValueData[i];
        clearingRes.vip_id = this.data.workRes.storeValueData[i].id;
      }
    }
    this.setData({
      clearingRes: clearingRes,
      vipCar: vipCar,
      modalShow: false
    })
  },
  // 关闭储值卡弹窗
  cancelTeam() {
    this.setData({
      modalShow: false,
    })
  },
  // 收款
  clearing() {
    let clearingRes = this.data.clearingRes;
    if(clearingRes.type) {
      if(clearingRes.total == this.data.currentPrice) {
        http.postRequest('/mini/v1/vip/clearing',clearingRes,res=> {
          if(res.code == 0) {
            wx.navigateBack({
              delta: 2
            })
          }
        })
      } else {
        wx.showToast({
          title: '款项未结清',
          icon: 'none'
        })
      }  
    } else {
      if(clearingRes.total == this.data.currentPrice) {
        http.postRequest('/mini/v1/work/clearing', clearingRes,res=> {
          if(res.code == 0) {
            wx.navigateBack({
              delta: 2
            })
          }
        })
      } else {
        wx.showToast({
          title: '款项未结清',
          icon: 'none'
        })
      }
    }
  },
  // 获取工单收款时的数据
  getwork(work_id, work_number) {
    http.postRequest('/mini/v1/work/data_clearing',{work_id: work_id, work_number: work_number},res=> {
      let currentPrice = res.data.workData.real_amount;
      let clearingRes = {};
      let vipCar = {};
      let aaa = [];
      for(let i in res.data.storeValueData) {
        if(Number(res.data.storeValueData[i].card_remain_amount) > Number(res.data.workData.real_amount)) {
          aaa.push(res.data.storeValueData[i]);
        }
      }
      clearingRes.total = 0;
      clearingRes.vip_id = ''
      if(aaa.length) {
        vipCar = aaa[aaa.length-1];
        clearingRes.total = res.data.workData.real_amount;
        clearingRes.vip_id = aaa[aaa.length-1].id;
        clearingRes.store_charge_against = res.data.workData.real_amount;
      }
      res.data.storeValueData = aaa;
      clearingRes.source_number = work_number;
      clearingRes.owner_id = res.data.workData.owner_id;
      clearingRes.work_id = work_id;
      clearingRes.car_id = res.data.workData.car.id;
      clearingRes.receivable_amount = res.data.workData.receivable_amount;
      clearingRes.real_amount = res.data.workData.real_amount;
      clearingRes.cash = 0;
      clearingRes.swiping_bank_card = 0;
      clearingRes.wechat_pay = 0;
      clearingRes.ali_pay = 0;
      clearingRes.bank_transfer = 0;
      clearingRes.other_pay = 0;
      clearingRes.clearing_discount = 0;
      clearingRes.remark = '';
      clearingRes.store_charge_against = '';
      
      this.setData({
        clearingRes: clearingRes,
        workRes: res.data,
        currentPrice: currentPrice,
        vipCar: vipCar
      })
    })
  },
  // 获取卡的收费
  getVip(document_number, document_id) {
    http.postRequest('/mini/v1/vip/document_info',{document_number: document_number, document_id: document_id}, res=> {
      let documentRes = {};
      documentRes.source_id = document_id;
      documentRes.source_number = document_number;
      documentRes.owner_id = res.data.owner_id;
      documentRes.card_tem_id = res.data.card_tem.id;
      documentRes.real_amount = res.data.card_real_amount;
      documentRes.receivable_amount = res.data.card_remain_amount;
      documentRes.discounts_amount = res.data.discounts_amount;
      documentRes.gender = res.data.owner.gender;
      documentRes.mobile = res.data.owner.mobile;
      documentRes.name =  res.data.owner.name;
      documentRes.cash = 0;
      documentRes.swiping_bank_card = 0;
      documentRes.wechat_pay = 0;
      documentRes.ali_pay = 0;
      documentRes.bank_transfer = 0;
      documentRes.other_pay = 0;
      documentRes.total = 0;
      documentRes.clearing_discount = 0;
      documentRes.remark = '';
      documentRes.type = 'clip';
      this.setData({
        clearingRes: documentRes,
        workRes: documentRes,
        currentPrice: res.data.card_real_amount
      })
    })
  }
})